/*
 * FormFillerRegister.cpp
 *
 *  Created on: 27/01/2009
 *      Author: desadoc
 */

#include <QStringList>
#include <QTextStream>

#include "FormFillerRegister.h"

#include "FormEditor/FormQuestion.h"

FormFillerRegister::FormFillerRegister(void)
{
	layout = new QVBoxLayout;
	setLayout(layout);
}

FormFillerRegister::FormFillerRegister(const FormFillerRegister& r)
{
	FormFillerField* f;
	layout = new QVBoxLayout;
	setLayout(layout);

	QList<FormFillerField*>::const_iterator it = r.fields.begin();

	for(; it != r.fields.end(); it++)
	{
		f = (*it)->getEmptyCopy();
		fields.push_back(f);
		layout->addWidget(f);
	}
}

QTextStream& operator<<(QTextStream& s, FormFillerRegister& r)
{
	QList<FormFillerField*>::const_iterator it;
	it = r.fields.begin();

	for(; it != r.fields.end(); it++)
	{
		s << (*it)->getQuestion() << "\n";
		s << (int) (*it)->getType() << "\n";
		s << (*it)->getAnswer() << "\n";
	}

	s << "END\n";

	return s;
}

QTextStream& operator>>(QTextStream& s, FormFillerRegister& r)
{
	char c;
	FormQuestion q;
	FormFillerField* f;

	while(!s.atEnd())
	{
		s >> q;
		f = new FormFillerField(q.getQuestion(), q.getType());
		r.fields.push_back(f);
		r.layout->addWidget(f);
		s >> c; // newline fix
	}

	return s;
}

FormFillerRegister* readFromTald(QTextStream* s)
{
	QString line;
	QString question;
	int tipo;
	FormFillerField* _f;
	FormFillerRegister* _new = new FormFillerRegister;

	line = s->readLine();

	while(line != "END")
	{
		question = line;

		line = s->readLine();
		tipo = line.toInt();

		line = s->readLine();
		//tipo = (*line.split('%', QString::SkipEmptyParts, Qt::CaseInsensitive).begin()).toInt();
		//line.remove(0,2);

		_f = new FormFillerField(question, (TIPOS)tipo);
		_f->setAnswer(line);
		_new->addField(_f);

		line = s->readLine();
	}

	return _new;
}

